package com.bqtt.tumor.demo.entity;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.tbwork.utility.toolbox.tbConvert;

import com.bqtt.tumor.demo.business.LabSheetService;
import com.bqtt.tumor.demo.common.utility.InlineTools;
 

@Entity
@Table(name="lab_sheet_result") 
public class LabSheetResult  implements IDBEntity{

	@Id 
    @Column(name="sheet_result_id")    
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
	private int sheet_result_id;
	
	@Column(name="case_id")    
	private int case_id;
	

	@Column(name="sheet_datetime")
	private Timestamp sheet_datetime;
	
	@Column(name="lab_sheet_name")    
	private String lab_sheet_name;

	@Column(name="result_value", length= 2000)   
	private String result_value;

	@Transient
	private String displayName;
	
	@Transient
	private int table_index;
	
	@Transient
	private int field_count;
	
	
	public int getTable_index() {
		return table_index;
	}

	public void setTable_index(int table_index) {
		this.table_index = table_index;
	}

	public int getField_count() {
		return field_count;
	}

	public void setField_count(int field_count) {
		this.field_count = field_count;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}
	
	@Transient
	private String displayResult;
	
	@Transient
	private String sheet_datetime_string=""; // for mvc
	
	public String getDisplayResult() {
		return displayResult;
	}

	public void setDisplayResult(String displayResult) {
		this.displayResult = displayResult;
	}

	@Override
	public Map<String, Object> getSQLValidAttributes() {
		// TODO Auto-generated method stub
       Map<String,Object> result = new HashMap<String,Object>();
		
		if(!InlineTools.isEmpty(lab_sheet_name))
		{
			result.put("lab_sheet_name", lab_sheet_name);
		}

		if(!InlineTools.isEmpty(lab_sheet_name))
		{
			result.put("lab_sheet_name", lab_sheet_name);
		}

		return result;
	}

	@Override
	public void fillStringField() {
		// TODO Auto-generated method stub
		displayResult ="";
		if(!lab_sheet_name.isEmpty())
		{
			 LabSheet labSheet = LabSheetService.getSheetByName(lab_sheet_name);
			 if(labSheet!=null)
			 {
				 String [] fields = labSheet.getFields().split("\\|");
				 String [] units = labSheet.getUnit().split("\\|");
				 String [] values =  result_value.split("\\|");
				 
				 if(fields.length == values.length)
				 {
					 for(int i=0;i<fields.length;i++)
					 {
						 displayResult = displayResult + fields[i]+": "+values[i]+" "+units[i]+"<br>";
					 }
				 }
				 
			 }
		}
		
		List<LabSheet> labsheets= LabSheetService.getList();
		if(labsheets!=null)
		{
			for(int i=0;i<labsheets.size();i++)
			{
				if(labsheets.get(i).getName().equals(lab_sheet_name))
				{
					table_index = i;
					String [] temp = labsheets.get(i).getFields().split("\\|");
					if(temp!=null)
					  field_count = temp.length;
					break;
				}
			}
		}
		
		
		if(sheet_datetime!=null)
		{
			sheet_datetime_string = InlineTools.getDateString(tbConvert.Timestamp2String(sheet_datetime));
		}
	}

	@Override
	public void fillDBfields() {
		// TODO Auto-generated method stub
		if(!InlineTools.isEmpty(sheet_datetime_string))
		{
			Timestamp temp =null;
			try{
				temp= tbConvert.StringToTimestamp(sheet_datetime_string+" 00:00:00");
			}
			catch(Exception e)
			{
				temp = new Timestamp(0);
			}
			
			sheet_datetime =temp;
		}
	}

	public int getSheet_result_id() {
		return sheet_result_id;
	}

	public void setSheet_result_id(int sheet_result_id) {
		this.sheet_result_id = sheet_result_id;
	}



	public int getCase_id() {
		return case_id;
	}

	public void setCase_id(int case_id) {
		this.case_id = case_id;
	}

	public String getLab_sheet_name() {
		return lab_sheet_name;
	}

	public void setLab_sheet_name(String lab_sheet_name) {
		this.lab_sheet_name = lab_sheet_name;
	}

	public Timestamp getSheet_datetime() {
		return sheet_datetime;
	}

	public void setSheet_datetime(Timestamp sheet_datetime) {
		this.sheet_datetime = sheet_datetime;
	}

	 
	public String getResult_value() {
		return result_value;
	}

	public void setResult_value(String result_value) {
		this.result_value = result_value;
	}

	public String getSheet_datetime_string() {
		return sheet_datetime_string;
	}

	public void setSheet_datetime_string(String sheet_datetime_string) {
		this.sheet_datetime_string = sheet_datetime_string;
	}

	public LabSheetResult() {
		super();
		displayName ="none";
		// TODO Auto-generated constructor stub
	}
	
}
